package Controller.User;



import Dto.User;
import MyException.MyException;
import Service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;


@WebServlet(name = "login", value = "/Login")
public class Login extends HttpServlet {
    private UserService userService = new UserService();
    //创建日志服务对象
//    private LogService logService = new LogService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String inputCode = request.getParameter("code");
        String rememberMe = request.getParameter("rememberMe");
//        String nameUser = request.getParameter("user");
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=utf8");
        String code = (String) session.getAttribute("captchaCode");
        try{
            if (!code.equals(inputCode)||inputCode==null){
                request.setAttribute("msg","验证码错误或为空");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
                return;
            }
            //超级用户登录
//            if(nameUser!=null&&nameUser.equals("manage")){
//                Super supe = userService.SuperLogin(username,password);
//                session.setAttribute("nicknameUser",supe.getNickname());
//                /*if("seven".equals(inputRem)){
//                    Cookie cookie = new Cookie("nickname",supe.getNickname());
//                    cookie.setMaxAge(7*24*60*60);
//                    response.addCookie(cookie);
//                }*/
//                request.getRequestDispatcher("/superHomePage.jsp").forward(request,response);
//                return;
//            }
            User user = userService.login(username,password);
            session.setAttribute("nickname",user.getNickname());
            if("rem".equals(rememberMe)){
                //设置cookie保存7天
                Cookie cookie = new Cookie("nickname",user.getNickname());
                cookie.setMaxAge(7*24*60*60);
                response.addCookie(cookie);
            }
//            Map<String, String[]> parameterMap = request.getParameterMap();
//            // 将请求参数转换为易于记录的格式
//            StringBuilder params = new StringBuilder();
//            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
//                for (String value : entry.getValue()) {
//                    params.append(entry.getKey()).append("=").append(value).append("&");
//                }
//            }
//            Log log = new Log();
//            log.setPath(request.getRequestURI());
//            log.setDate(LocalDateTime.now());
//            log.setParameter(params.toString());
//            log.setUsername(user.getNickname());
//            logService.addLog(log);
            request.getRequestDispatcher("/homepage.jsp").forward(request,response);
        }catch (MyException e){
            request.setAttribute("msg",e.getMessage());
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }catch (SQLException e){
            e.printStackTrace();
            System.out.println("sql异常");
        }
    }
}
